<template>
  <div class="page">
    <nav-bar title="基础信息审核"></nav-bar>
    <div class="page-main" style="margin-top: 44px">
      <van-form validate-trigger="onSubmit" :show-error="false" :label-width="120" @submit="onSubmit">
        <van-field label="驳回原因" readonly :border="false" />
        <van-field
          v-model="formParams.rejectReason"
          rows="5"
          autosize
          type="textarea"
          maxlength="200"
          show-word-limit
          class="van-field__value--textarea"
          placeholder="请输入驳回原因……"
          :border="false"
          :rules="rules.rejectReason"
        />
        <div style="margin: 60px 16px">
          <van-row :gutter="20">
            <van-col :span="12">
              <van-button block native-type="button" @click="$router.go(-1)">取消</van-button>
            </van-col>
            <van-col :span="12">
              <van-button block type="info" native-type="submit" :disabled="buttonDisabled">提交</van-button>
            </van-col>
          </van-row>
        </div>
      </van-form>
    </div>
  </div>
</template>
<script>
import Resident from '@/api/resident'
export default {
  data() {
    return {
      buttonDisabled: false,
      formParams: {
        code: this.$route.query.code,
        rejectReason: ''
      },
      rules: {
        rejectReason: [
          { required: true, message: '请输入驳回原因' },
          {
            validator: val => {
              return val.length >= 10
            },
            message: '驳回原因至少输入10个字'
          }
        ]
      }
    }
  },
  methods: {
    onSubmit() {
      let api = ''
      if (
        this.$route.query.baseType === '1' ||
        this.$route.query.baseType === '2' ||
        this.$route.query.baseType === '3'
      ) {
        // 人员驳回 - 本地、非本地、境外
        api = 'residentReject'
        this.formParams.personCode = this.formParams.code
      } else if (this.$route.query.baseType === '8') {
        // 车辆驳回
        api = 'carReject'
        this.formParams.carCode = this.formParams.code
      } else if (this.$route.query.baseType === '9') {
        //  宠物驳回
        api = 'petReject'
      }
      this.buttonDisabled = true
      Resident.BaseInfo[api](this.formParams)
        .then(res => {
          const { status } = res.data
          if (status === 0) {
            this.$dialog
              .alert({
                title: '提示',
                message: '该审核信息已驳回'
              })
              .then(() => {
                this.$router.go(-2)
              })
          } else {
            this.$toast.fail(res.data.message)
          }
          this.buttonDisabled = false
        })
        .catch(() => {
          this.buttonDisabled = false
        })
    }
  }
}
</script>
